import sys
from PyQt4 import QtGui,QtCore,uic
from urllib import request,parse
import json
import zlib
import time

myw = uic.loadUiType("weather2.ui")[0]

class mainWindows(QtGui.QMainWindow,myw):
    def __init__(self,parent=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.setupUi(self)
        self.timer1 = QtCore.QTimer()
        self.timer1.timeout.connect(self.showtime)
        self.timer1.start(1000)
        self.pushButton.clicked.connect(self.getweatherinfo)
        self.timer_getweather = QtCore.QTimer()
        self.timer_getweather.timeout.connect(self.getweatherinfo)
        #self.timer_getweather.start(1000)
        self.getweatherinfo()
        self.start_getwether_timer()
        self.comboBox.currentIndexChanged.connect(self.start_getwether_timer)


    def start_getwether_timer(self):
        t = int(self.comboBox.currentText())*60000
        self.timer_getweather.start(t)
        self.getweatherinfo()
        print(t)
    def showtime(self):
        time_str = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
        self.lcdNumber.display(time_str)

    def getweatherinfo(self):
        url = "http://wthrcdn.etouch.cn/weather_mini?city="
        cityname = parse.quote(self.lineEdit.text())
        # print(url+cityname)
        #try:
        con = request.urlopen(url+cityname).read()
        con_uz = zlib.decompress(con,16+zlib.MAX_WBITS).decode()
        # print(con_uz)
        con_json = json.loads(con_uz)
        self.statusBar().showMessage(con_json['desc'])
        if con_json['desc'] == 'OK':
            self.label.setText(con_json['data']['wendu'])
            self.textBrowser.setText(con_json['data']['ganmao'])
            self.label_6.setText(con_json['data']['yesterday']['date'])
            self.label_3.setText(con_json['data']['yesterday']['type'])
            self.label_4.setText(con_json['data']['yesterday']['high'].split()[1]+'/'+con_json['data']['yesterday']['low'].split()[1])
            self.label_5.setText(con_json['data']['yesterday']['fx'])
            self.label_7.setText(con_json['data']['forecast'][0]['date'])
            self.label_8.setText(con_json['data']['forecast'][0]['type'])
            self.label_9.setText(con_json['data']['forecast'][0]['high'].split()[1]+'/'+con_json['data']['yesterday']['low'].split()[1])
            self.label_10.setText(con_json['data']['forecast'][0]['fengxiang'])
            self.label_27.setText(con_json['data']['forecast'][1]['date'])
            self.label_28.setText(con_json['data']['forecast'][1]['type'])
            self.label_29.setText(con_json['data']['forecast'][1]['high'].split()[1]+'/'+con_json['data']['yesterday']['low'].split()[1])
            self.label_30.setText(con_json['data']['forecast'][1]['fengxiang'])
            self.label_31.setText(con_json['data']['forecast'][2]['date'])
            self.label_32.setText(con_json['data']['forecast'][2]['type'])
            self.label_33.setText(con_json['data']['forecast'][2]['high'].split()[1]+'/'+con_json['data']['yesterday']['low'].split()[1])
            self.label_34.setText(con_json['data']['forecast'][2]['fengxiang'])
            self.label_35.setText(con_json['data']['forecast'][3]['date'])
            self.label_36.setText(con_json['data']['forecast'][3]['type'])
            self.label_37.setText(con_json['data']['forecast'][3]['high'].split()[1]+'/'+con_json['data']['yesterday']['low'].split()[1])
            self.label_38.setText(con_json['data']['forecast'][3]['fengxiang'])
            self.label_39.setText(con_json['data']['forecast'][4]['date'])
            self.label_40.setText(con_json['data']['forecast'][4]['type'])
            self.label_41.setText(con_json['data']['forecast'][4]['high'].split()[1]+'/'+con_json['data']['yesterday']['low'].split()[1])
            self.label_42.setText(con_json['data']['forecast'][4]['fengxiang'])
            self.label_48.setText(time.asctime( time.localtime(time.time())))
            self.label_48.adjustSize()
            self.lineEdit.setText(con_json['data']['city'])
        else:
            QtGui.QMessageBox.information(self,"提示",self.tr("请填写正确的中文城市名！！！"))








app = QtGui.QApplication(sys.argv)
w = mainWindows()
w.show()
sys.exit(app.exec_())
